/**
 * Help loading and cache adm code. If code was loaded return it; 
 * Otherwise, loads it first then cache and return it.
 * 
 * 1. Only fetch one kind of adm code, invoke 
 * Ext.create("byb.AdmCode").get("codeType");
 * 
 * 2. Fetch many kinds of adm code at one time, invoke 
 * Ext.create("byb.AdmCode").get(["codeType_1","codeType_2",...]).
 * 
 */
Ext.define("byb.AdmCode", {
	code : {},
	get : function(codeType) {
		//console.log('CodeType:' + codeType);
		var result = {};
		if(codeType instanceof Array){
			//fetch one kind of adm code
			for(var type in codeType){
				if (codeType && (!this.code[codeType[type]] || this.code[codeType[type]].length < 1)) {
					this.query(codeType[type]);
				}
				result[codeType[type]] = this.code[codeType[type]];
			}
		} else {//fetch many kind of adm code at one time
			if (codeType && (!this.code[codeType] || this.code[codeType].length < 1)) {
				this.query(codeType);
			}
			result = this.code[codeType];
		}
		
		if(codeType === 'BaseWineType' && byb.Constant.type === 'JX'){
			var baseWineType = [];
			Ext.each(result,function(item){
				if(item.code !== 'JX_WINE'){
					baseWineType.push(item);
				}
			});
			return baseWineType;
        }else{
        	return result;
        }
	},
	query : function(codeType) {
		Ext.Ajax.request({
			url : byb.Constant.APIRoot + '/queryAdmCode',
			params : {
				"codeType" : codeType
			},
			method : 'GET',
			async : false,
			code : this.code,
			success : function(response, opts) {
				try{
					var obj = Ext.decode(response.responseText);
					if (obj && obj.hasOwnProperty("results")) {; 
						opts.code[codeType] = obj.results;
					}else {
						opts.code[codeType] = [];
					}
				}catch(e){
					//
				}

			},
			failure : function(response, opts) {
				opts.code[codeType] = [];
				Ext.Msg.show({
					title : "提示",
					msg : "数据字典加载失败",
					animal : "id1",
					width : 300,
					height : 100,
					closable : true,
					icon : Ext.Msg.WARNING
				});
			}
		});
		
	},
	clear : function() {
		this.code = {};
	}

});
